package com.levtech.moviemanager.movie;

import com.levtech.moviemanager.MainActivity;
import com.levtech.moviemanager.MovieManagerApp;
import com.levtech.moviemanager.R;
import com.levtech.moviemanager.db.DataStore;
import com.levtech.moviemanager.types.Genre;
import com.levtech.moviemanager.types.Studio;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class EditMovieActivity extends Activity
{
	
	private DataStore store;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_movie);
		this.store = ((MovieManagerApp)this.getApplication()).getStore();
        populateGenre();
        populateStudio();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit_movie, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem selection)
	{
		switch(selection.getItemId())
		{
			case R.id.add_movie:	startActivity(new Intent(this, AddMovieActivity.class));
									return true;
									
			case R.id.view_movie:	startActivity(new Intent(this, ViewMovieActivity.class));
									return true;
									
			default: return super.onOptionsItemSelected(selection);
		}
	}
	
	public void editMovie(View view)
	{
		// create movie from user filled in fields
		Movie m = new Movie(((EditText)findViewById(R.id.titleField)).getText().toString(),
				((EditText)findViewById(R.id.descField)).getText().toString(),
				((Spinner)findViewById(R.id.genreField)).getSelectedItem().toString(),
				""/*((Spinner)findViewById(R.id.formatField)).getSelectedItem().toString()*/,
				((EditText)findViewById(R.id.castField)).getText().toString(),
				((Spinner)findViewById(R.id.studioField)).getSelectedItem().toString());
		// dump into database
		store.insertMovie(m);
		
		//return to main screen
		startActivity(new Intent(this, MainActivity.class));
	}
	
	private void populateGenre()
	{
		Spinner genre = (Spinner) findViewById(R.id.genreField);
		genre.setAdapter(new ArrayAdapter<Genre>(this, android.R.layout.simple_spinner_dropdown_item,
				Genre.values()));
	}
	
    private void populateStudio()
    {
		Spinner studio = (Spinner) findViewById(R.id.studioField);
		studio.setAdapter(new ArrayAdapter<Studio>(this, android.R.layout.simple_spinner_dropdown_item,
				Studio.values()));
	}

}
